package com.gargi.crimepatrol.main;

import com.gargi.crimepatrol.network.CrimePatrol;
import com.gargi.crimepatrol.network.ImageOperation;
import com.gargi.crimpatrol.utils.Constant;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Style;

public class ImageTextCellRenderer extends Container implements
		ListCellRenderer {
	
	private Label thumbnailLabel;
	private TextArea titleText;
	private LiveTvList list;
	
	private boolean btnSelected;
	
	public ImageTextCellRenderer(LiveTvList list) {
		this.list = list;
		setPreferredH(100);
		setPreferredW(Display.getInstance().getDisplayWidth());
		InitializeComponents();
		setLayout();
	}
	
	private void InitializeComponents() {

        thumbnailLabel = new Label();
        
        titleText = new TextArea();
        titleText.setEditable(false);
        titleText.setUIID("Label");
        //titleText.setColumns(3);
        Style textStyle =  titleText.getStyle();
        textStyle.setFont(Constant.SMALL_BOLD_FONT);
      
	}
	
	private void setLayout() {
		Container thumbnailcontainer = new Container();
		thumbnailcontainer.addComponent(thumbnailLabel);
		
		/*Container nameContainer = new Container();
		nameContainer.setLayout(new BorderLayout());
		nameContainer.addComponent(BorderLayout.CENTER, nameLabel);*/
		
		BoxLayout layout = new BoxLayout(BoxLayout.X_AXIS);
		setLayout(layout);
		addComponent(thumbnailcontainer);
		addComponent(titleText);

	}
	
	public boolean isBtnSelected() {
		return btnSelected;
	}

	public void setBtnSelected(boolean btnSelected) {
		this.btnSelected = btnSelected;
	}

	public Component getListCellRendererComponent(List list, Object object,
			int index, boolean isSelected) {
		final CrimePatrol category = (CrimePatrol)object;
		
		titleText.setText(category.getTitle());

		Image thumbnail = category.getThumbnail();
        
        if (category.getThumbnail() == null) {
        	thumbnailLabel.setIcon(Constant.DEFAULT_IMAGE);
        	if(!category.isLoadingThumbnail()) {
            	category.loadImageFromUrl(new ImageOperation.Listener() {
    				
    				
    				public void imageReceived(String url, Image image) {
    					category.setLoadingThumbnail(false);
    					Image resizedImage =  Constant.getImageResize(image, 70, 80);
    					category.setThumbnail(resizedImage);
    					thumbnailLabel.setIcon(image);
    					ImageTextCellRenderer.this.list.getLiveCatView().repaint();
    				}

					public void onNetworkFailure() {
					}
    			}, category.getIcon());
        	}
        }else{
        	thumbnailLabel.setIcon(thumbnail);
        }

		return this;
	}	

	public Component getListFocusComponent(List list) {
		return null;
	}

}
